﻿using Dapper;
using Entity.Utils;
using EntityLibrary.Business.ReportInfo.Entity;
using EntityLibrary.Business.ReportInfo.Sql;
using EntityLibrary.DomainCheck;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;

namespace EntityLibrary.Business.ReportInfo
{
    public class ReportService
    {
        readonly string connStr = Decryption.StrDecrypt(ConfigurationManager.ConnectionStrings["HISDATA"].ConnectionString);
        readonly string connLisStr = Decryption.StrDecrypt(ConfigurationManager.ConnectionStrings["LISData"].ConnectionString);

        //查询检查报告单
        public List<CheckReport> GetCheckReport(int CallNo, string CallName, string patientId, int printStatus, string applyNum = null)
        {
            if (default(KeyValuePair<string, string>).Equals(
            CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
            ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }
            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetCheckReport.sql");

            var sqlParam = new DynamicParameters();
            sqlParam.Add("@DataSource_In", Decryption.StrDecrypt(ConfigurationManager.ConnectionStrings["PacksConfig"].ConnectionString));
            sqlParam.Add("@PatientId_In", patientId);
            sqlParam.Add("@ApplyNum_In", applyNum);
            sqlParam.Add("@Flag_In", printStatus);
            var checkReportList = conn.Query<CheckReport>(sqlStr, sqlParam).ToList();
            conn.Close();
            return checkReportList;
        }

        //查询检验报告单(查询门诊部分)
        public List<InspectReportCount> GetInspectRecordByMZNum(int CallNo, string CallName, string patientId)
        {
            if (default(KeyValuePair<string, string>).Equals(
           CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
           ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }
            //RegistSheet
            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetInspectRecordByMZNum.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@PatientId_In", patientId);
            var inspectNumList = conn.Query<GetMZNumRecords>(sqlStr, sqlParam).ToList();
            if (inspectNumList.Count.Equals(0))
                throw new InvalidOperationException("没有查询到就诊记录");

            var connLis = new SqlConnection(connLisStr);
            List<InspectReportCount> inspectList = new List<InspectReportCount>();
            string sqlStr2 = @"EXEC UTSP_GET_REPORT_COUNT @patientCode ,@patientName";
            foreach (GetMZNumRecords record in inspectNumList)
            {
                var sqlParam2 = new DynamicParameters();
                sqlParam2.Add("@patientCode", record.MZNum);
                sqlParam2.Add("@patientName", record.PatientName?.Trim());
                InspectReportCount reportCounts = connLis.Query<InspectReportCount>(sqlStr2, sqlParam2).FirstOrDefault();
                if (reportCounts != null)
                    inspectList.Add(reportCounts);
            }
            conn.Close();
            return inspectList;
        }
    }
}
